﻿using AutoMapper.Internal.Mappers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Himp.Erp
{
    /// <summary>
    /// 物料仓储
    /// </summary>
    public class MatWhseService : CrudAppService<MatlWhse, MatlWhseDto, Guid, MatlWhseInput, MatlWhseCreateOrUpdateDto, MatlWhseCreateOrUpdateDto>, IMatlWhseService
    {
        private IRepository<MatlWhse, Guid> _materialWhseRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectConfigRepository"></param>
        public MatWhseService(IRepository<MatlWhse, Guid> materialWhseRepository) 
            : base(materialWhseRepository)
        {
            _materialWhseRepository = materialWhseRepository;
        }

        /// <summary>
        /// 获取物料信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<PagedResultDto<MatlWhseDto>> GetListAsync(MatlWhseInput input)
        {
            var iQuery = await _materialWhseRepository.GetQueryableAsync();

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<MatlWhse>, List<MatlWhseDto>>(iQuery.ToList());

                return new PagedResultDto<MatlWhseDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<MatlWhse>, List<MatlWhseDto>>(items.ToList());

                return new PagedResultDto<MatlWhseDto>(totalCount, itemsDto);
            }
        }
    }
}
